<?php

declare (strict_types = 1);

namespace app\store\service\statistics\data;

use app\common\model\HomeCallLog;
use app\common\service\BaseService;
use app\store\model\User;


/**
 * 近30日走势
 * Class Trade7days
 * @package app\store\service\statistics\data
 */
class TradeMonth extends BaseService
{
    /**
     * 近30日走势
     * @return array
     */
    public function getTransactionTrend(): array
    {
        // 最近30天日期
        $lately30days = $this->getLately30days();
        return [
            'date' => $lately30days,
            'loginAction' => $this->getLoginUserCount($lately30days),
            'registerAction' => $this->getRegisterUserCount($lately30days),
        ];
    }


    /**
     * 最近30天日期
     */
    private function getLately30days(): array
    {
        // 获取当前周几
        $date = [];
        for ($i = 0; $i < 30; $i++) {
            $date[] = date('Y-m-d', strtotime('-' . $i . ' days'));
        }
        return array_reverse($date);
    }


    /**登录行为
     * @param $lately30days
     * @return array
     */
    public function getLoginUserCount($lately30days):array
    {
        $data = [];
        foreach ($lately30days as $day) {
            $condition = [
                ['call_time', '>=', strtotime($day)],
                ['call_time', '<', strtotime($day) + 86400],
            ];
            $num = HomeCallLog::where($condition)->count();
            $data[] = $num;
        }
        return $data;
    }



    /**注册行为
     * @param $lately30days
     * @return array
     */
    public function getRegisterUserCount($lately30days):array
    {
        $data = [];
        foreach ($lately30days as $day) {
            $day .= ' 00:00:00';
            $condition = [
                ['created_at', '>=', $day],
                ['created_at', '<', date('Y-m-d H:i:s',strtotime($day) + 86400)],
            ];
            $num = User::where($condition)->count();
            $data[] = $num;
        }
        return $data;
    }




}